package com.bdqn;

import com.pojo.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Sjlx1 {
    public static void main(String[] args) {
        Class clazz = Person.class;
        String fullName = clazz.getName();
        String simpleName = clazz.getSimpleName();
        System.out.println("以下是" + fullName + "类型信息");
        System.out.println("----------------------");

        System.out.println("类名是:" + simpleName);
        System.out.println("-----------------------");

        Package pkg = clazz.getPackage();
        System.out.println(simpleName + "定义在:" + pkg.getName() + "包中");
        System.out.println("--------------------------");

        Class superClass = clazz.getSuperclass();
        System.out.println(simpleName + "类的超类是:" + superClass.getName());
        System.out.println("---------------------------");

        int modifier = clazz.getModifiers();
        System.out.println(simpleName + "类的修饰符是");
        if ((modifier & Modifier.PUBLIC) == Modifier.PUBLIC) {
            System.out.println("\t访问修饰符是:public");
        }
        System.out.println("-----------------------------");
        Constructor[] cons = Person.class.getDeclaredConstructors();

        System.out.println("=======构造方法=======");
        for (Constructor con : cons) {
            System.out.println("访问修饰符:");
            modifier = con.getModifiers();
            if ((modifier & Modifier.PUBLIC) == Modifier.PUBLIC)
                System.out.println("public");
            else if ((modifier & Modifier.PROTECTED) == Modifier.PROTECTED)
                System.out.println("protected");
            else if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE)
                System.out.println("private");
            else
                System.out.println("default(package)");


            Class[] params = con.getParameterTypes();
            if (params.length == 0) {
                System.out.println("该构造方法没有参数");
                System.out.println("--------------------------");
            } else {
                System.out.print("该构造方法的参数列表为:[");
                for (int i = 0; i < params.length; i++) {
                    if (i != 0)
                        System.out.print(",");
                        System.out.print(params[i].getName());

                    System.out.println("]");
                }
                System.out.println("---------------------------");
            }
        }
        Field[] fields=Person.class.getDeclaredFields();
        System.out.println("===========属性展示===========");
        for (Field field:fields){
            System.out.println("属性名:"+field.getName());
            System.out.println("类型:"+field.getType().getName());
            System.out.println("访问修饰符:");
            int modifiers=field.getModifiers();
            if((modifiers&Modifier.STATIC)==Modifier.STATIC)
                System.out.println("这是一个静态属性");
            if((modifiers&Modifier.FINAL)==Modifier.FINAL)
                System.out.println("这是一个final属性");
            System.out.println("--------------------------");
        }

        Method[] methods=Person.class.getDeclaredMethods();
        System.out.println("============方法展示===========");
        for (Method method: methods) {
            System.out.println("方法名:"+method.getName());
            System.out.println("返回值类型:"+method.getReturnType().getName());
            Class declaringClass=method.getDeclaringClass();
            System.out.println("方法声明在:"+declaringClass.getName()+"中");
            System.out.println("---------------------------");
        }
    }
}
